// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "./ComponentBase";
import Message from "./Message";
import UI_Manager from "./UI_Manager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class HpControl extends ComponentBase {
    hp: number = 100;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        // 注册为UI的消息接收者
        UI_Manager.instance.RegisterReceiver(this);
    }

    ReceiveMsg(msg: Message) {
        console.log(msg);
        if (msg.Content == '血量减少') {
            this.changeHP(1);
        }
        
    }

    // 改变血量
    changeHP(attack) {
        this.hp -= attack;
        this.node.getChildByName('hpNum').getComponent(cc.Label).string = String(this.hp);
    }

    // update (dt) {}
}
